# -*- coding: UTF-8 -*- 
#name = xiang
#python 3.x

'''
1.先定义一个函数计算总数和平均数
2.根据总分和平均分数排序
3.根据循环的次数添加名次
'''

new_list=[]
report_list=[]
start = 1
count = 0

def jisuan(data):
    data=data.split()
    total = sum(int(i) for i in data[1:10])
    avg = round(float(total / 9),ndigits=2)
    data.append(total)
    data.append(avg)
    return data


with open('report.txt','+r',encoding="utf-8") as f :
    for line in f.readlines():
        count += 1
        line=jisuan(line)
        new_list.append(line)
        new_list.sort(key = lambda x:(x[-2],x[-1]),reverse=True)

    for lst in new_list:
        for score in lst[1:10] :
            if int(score) < 60 :
                lst[lst.index(score)]="不及格"
            else :
                lst[lst.index(score)] = str(lst[lst.index(score)])
        for i in lst[10:12]:
            lst[lst.index(i)] = str(lst[lst.index(i)])

        report_list.append(lst)

new_report = open('report01.txt','w',encoding='utf-8')
title = ["名次","姓名","语文","数学","英语","物理","化学","生物","政治","历史","地理","总分","平均分"]
new_report.write(" ".join(title)+"\n")
for everyone in report_list :
    if start <= count :
        everyone.insert(0, str(start))
        start += 1
        print(everyone)
    new_report.write(" ".join(everyone)+"\n")
new_report.close()





